<template>
  <div>
    <div class="login-title">面经注册</div>
    <van-form @submit="onSubmit">
      <van-field
        v-model="username"
        name="用户名"
        label="用户名"
        placeholder="用户名"
        :rules="[{ required: true, message: '请填写用户名' }]"
      />
      <van-field
        v-model="password"
        type="password"
        name="密码"
        label="密码"
        placeholder="密码"
        :rules="[{ required: true, message: '请填写密码' }]"
      />
      <div style="margin: 16px">
        <van-button round block type="info" native-type="submit"
          >注册</van-button
        >
      </div>
    </van-form>
    <router-link to="/login">有账号，去登录</router-link>
  </div>
</template>

<script>
import axios from "axios";
export default {
  data() {
    return {
      username: "",
      password: "",
    };
  },
  methods: {
    async onSubmit() {
      if (!this.username || !this.password) {
        return;
      }
      let res = await axios({
        method: "post",
        url: "http://interview-api-t.itheima.net/h5/user/register",

        data: {
          username: this.username,
          password: this.password,
        },
      });

      console.log(res);

      if (res.data.code == 10000) {
        this.$router.push({ path: "/login" });
        alert("注册成功");
      }
    },
  },
};
</script>

<style lang="less" scoped>
.login-container button {
  width: 300px;
  background-color: #07c160;
  color: white;
  height: 40px;
  border: none;
}
.login-container input {
  border: none;
}
</style>
